<?php

/**
 * Created by PhpStorm.
 * User: wu
 * Date: 2017/6/16
 * Time: 9:43
 */
namespace V1;
class Product extends \ResourceClass
{
    public $isActive = true;
    public $remark = '商品接口';

    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['article_id'])) {
            return '文章id不能为空';
        }

        try {
            $product_class = new \Orm\Product\ProductClass();
            $product_class['article_id'] = $data['article_id'];
            $product_class['name'] = isset($data['name']) ? $data['name'] : '';
            $product_class['describe'] =  isset($data['describe']) ? $data['describe'] : '';;
            $product_class['unit_price'] =  isset($data['unit_price']) ? $data['unit_price'] : '';
            $product_class->insert();
//            $product_class->loadById($product_class['id']);
            $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
            $article_content_class['article_id'] = $data['article_id'];
            $article_content_class['type'] = $article_content_class::TYPE_PRODUCT;
            $article_content_class['content'] = $product_class['id'];
            $article_content_class->insert();
            // todo use mysql transaction

            $result = array(
                'id' => $product_class['id'],
                'article_id' => $product_class['article_id'],
                'name' => $product_class['name'],
                'describe' => $product_class['describe'],
                'unit_price' => $product_class['unit_price'],
                );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!isset($req['id'])) {
            return 'require ID';
        }

        try {
            $product_class = new \Orm\Product\ProductClass();
            $product_class->loadById($req['id']);
            $product_class->completeImageList();
            $result = array(
                'id' => $product_class['id'],
                'article_id' => $product_class['article_id'],
                'name' => $product_class['name'],
                'describe' => $product_class['describe'],
                'unit_price' => $product_class['unit_price'],
                'image_list' => $product_class['image_list'],
                );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }

        return '';
    }

    public function put(array $req, array $post, &$result)
    {
        if (!isset($req['id'])) {
            return 'require ID';
        }
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['name'])) {
            return '商品名称不能为空';
        }
        if (!isset($data['unit_price'])) {
            return '商品名称不能为空';
        }

        try {
            $product_class = new \Orm\Product\ProductClass();
            $product_class->loadById($req['id']);
            if (isset($data['name'])) {
                $product_class['name'] = $data['name'];
            }
            if (isset($data['describe'])) {
                $product_class['describe'] =  $data['describe'];
            }
            if (isset($data['status'])) {
                $product_class['status'] =  $data['status'];
            }
            if (isset($data['unit_price'])) {
                $product_class['unit_price'] =  $data['unit_price'];
            }
            $product_class->update();
            if (isset($data['image_id_list']) && $data['image_id_list'] != null && $data['image_id_list'] != 'null') {
                if(strlen($data['image_id_list'])) {
                    $image_list = json_decode($data['image_id_list']);
                    $product_class->updateImage($image_list);
                }
            }

            $result = array(
                'id' => $product_class['id'],
                'article_id' => $product_class['article_id'],
                'name' => $product_class['name'],
                'describe' => $product_class['describe'],
                'unit_price' => $product_class['unit_price'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }
}